// 工厂方法, 用于创建 一个 store module
import moduleCreator from '../moduleCreator'

import api from '@/api'

// moduleName 指向了
const modelName = 'sale.order'

const Model = api.env(modelName)

const baseModel = {
  async find_register(payload) {
    const { domain } = payload
    const res = await Model.search_one({
      ...payload,
      domain: {
        ...domain,
        state: ['draft', 'sale'],
        'analytic_account_id.group_id.name': 'register'
      }
    })
    console.log('find_register,', res)
    return res
  },

  async create_register(payload) {
    const {
      values: { partner_id, analytic_account_partner_id },
      fields
    } = payload
    // const { partner_id } = api.get_userinfo()

    const analytic_account_partner = await api
      .env('res.partner')
      .browse_one(analytic_account_partner_id, {
        fields: { user_ids: null, sale_order_template_id: null }
      })

    const analytic_account = await api
      .env('account.analytic.account')
      .search_one({
        domain: {
          partner_id: analytic_account_partner_id,
          'group_id.name': 'register'
        },

        fields: {}
      })

    const values = {
      partner_id,
      sale_order_template_id: analytic_account_partner.sale_order_template_id,
      analytic_account_id: analytic_account.id
    }
    // console.log('create_register,', values, fields)
    const res = await Model.create(values, { fields })
    // console.log('create_register,', res)

    return res
  },

  async action_confirm(payload) {
    const { id, fields } = payload
    const res = await Model.call('action_confirm', [id])
    console.log('action_confirm,', res)
    const res2 = await Model.browse_one(id, { fields })
    console.log('action_confirm,', res2)

    return res2
  },

  async action_done(payload) {
    const { id, fields } = payload
    const res = await Model.call('action_done', [id])
    console.log('action_done,', res)
    const res2 = await Model.browse_one(id, { fields })
    console.log('action_done,', res2)
    return res2
  },

  async create_invoices(payload) {
    const { id, fields } = payload
    const wizard_model = 'sale.advance.payment.inv'

    const wizard = await Model.wizard_create(id, {
      model: wizard_model
    })
    console.log('wizard_create,', wizard)

    const res2 = await Model.wizard_submit(id, {
      model: wizard_model,
      values: { id: wizard.id },
      method: 'create_invoices'
    })
    console.log('wizard_submit,', res2)
    const res = await Model.browse_one(id, { fields })
    console.log('browse_one,', res)
    return res
  }
}

const fields = {
  name: null,
  state: null,
  state2: null,
  date_order: null,
  partner_id: null,
  analytic_account_partner_id: null,
  analytic_account_id: {
    group_id: null
  },
  sale_order_template_id: null,
  amount_total: null,

  order_line: {
    name: null,
    product_id: null,
    product_template_id: null,
    product_uom: null,
    price_unit: null,
    product_uom_qty: null
  },
  invoice_count: null,
  invoice_ids: {
    name: null,
    amount_total: null,
    state: null,
    invoice_payment_state: null,
    invoice_line_ids: null
  },
  invoice_status: null,
  invoice_id: {
    name: null,
    amount_total: null,
    state: null,
    invoice_payment_state: null,
    invoice_line_ids: null
  },
  purchase_order_id: null
}

const config = {
  model: modelName,
  fieldsForSearch: fields,
  fieldsForBrowse: fields
}

const state = {
  invoice: {}
}

const mutations = {
  SET_invoice: (state, row) => {
    state.invoice = row
  }
}

const actions = {
  async setList({ commit, state, dispatch }, values) {
    console.log('SO , setList', values)

    const state2_names = {
      none: 'none',
      draft: '草稿',
      done: '完成',
      no_invoice: '无需开票',
      to_invoice: '待开票',
      not_paid: '待支付',
      in_payment: '支付中',
      paid: '已支付'
    }

    const vals = values.map(item => {
      // const invoice_id = item.invoice_ids__objects.length
      //   ? item.invoice_ids__objects[0]
      //   : {}

      return {
        ...item,
        // invoice_id,
        state2_name: state2_names[item.state2]
      }
    })

    return dispatch('_setList', vals)
  },

  async after_browse_one({ commit, state, dispatch }, payload) {
    const so = payload
    commit('SET_DataDict', so)
    const invs = so.invoice_ids__objects
    const invoce = invs && invs.length ? so.invoice_ids__objects[0] : {}
    await this.dispatch('AccountMove/setDict', invoce)

    return new Promise(resolve => {
      resolve(so)
    })
  },

  async browse_one({ commit, state, dispatch }, payload) {
    const so = await dispatch('_browse_one', payload)
    return dispatch('after_browse_one', so)
  },

  async register_by_cust({ commit, state, dispatch }, payload) {
    const {
      channel: { me_partner_id, opp_partner_id }
    } = payload

    const payload2 = {
      partner_id: me_partner_id,
      analytic_account_partner_id: opp_partner_id
    }

    const so = await dispatch('find_register', payload2)
    if (so.id) {
      return new Promise(resolve => {
        resolve(so)
      })
    }
    return dispatch('create_register', payload2)
  },

  async register_by_emp({ commit, state, dispatch }, payload) {
    const {
      channel: { me_partner_id, opp_partner_id }
    } = payload

    const payload2 = {
      partner_id: opp_partner_id,
      analytic_account_partner_id: me_partner_id
    }

    return dispatch('find_register', payload2)
  },

  async find_register({ commit, state, dispatch }, payload) {
    const { partner_id, analytic_account_partner_id } = payload

    const so = await baseModel.find_register({
      domain: {
        partner_id,
        'analytic_account_id.partner_id': analytic_account_partner_id
      },
      fields
    })

    // console.log('find_register', so)
    return dispatch('after_browse_one', so)
  },

  async create_register({ commit, state, dispatch }, payload) {
    const { partner_id, analytic_account_partner_id } = payload
    const so = await baseModel.create_register({
      values: { partner_id, analytic_account_partner_id },
      fields
    })
    return dispatch('after_browse_one', so)
  },

  async action_confirm({ commit, state, dispatch }, payload) {
    const res = await baseModel.action_confirm({ ...payload, fields })
    // console.log('action_confirm', res)
    commit('SET_DataDict', res)
    return new Promise(resolve => {
      resolve(res)
    })
  },

  async create_invoices({ commit, state, dispatch }, payload) {
    const res = await baseModel.create_invoices({ ...payload, fields })
    console.log('create_invoices', res)
    return dispatch('after_browse_one', res)
  },

  async action_done({ commit, state, dispatch }, payload) {
    const res = await baseModel.action_done({ ...payload, fields })
    // console.log('action_confirm', res)
    commit('SET_DataDict', res)
    return new Promise(resolve => {
      resolve(res)
    })
  }
}

export default moduleCreator(config, {
  state,
  mutations,
  actions
})

// // 这是最简单的写法
// export default moduleCreator(modelName)

// 如果有扩展, 则按照下面的写法
// const state = {
//   //
// }
// const mutations = {
//   //
// }
// const actions = {
//   //
// }

// export default moduleCreator(modelName, {
//   namespaced: true,
//   state,
//   mutations,
//   actions
// })

// 如果 不继承, 用下面的写法
// const state = {
//   //
// }
// const mutations = {
//   //
// }
// const actions = {
//   //
// }

// export default {
//   namespaced: true,
//   state,
//   mutations,
//   actions
// }
